/*
 *      net_list.cpp
 *
 *      Copyright 2009 AzureSky <AzureSky.fedora@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "net_list.h"

net_list::net_list()
{
}

net_list::~net_list()
{
}

void net_list::read()
{
	read_file("/proc/net/tcp","TCP");
	read_file("/proc/net/udp","UDP");
}

void net_list::refresh()
{
	clear();
	read();
}

void net_list::read_file(std::string path,std::string type)
{
    std::ifstream file(path.c_str());
    if(file.peek() == EOF){
              std::cout << " error " << std::endl;
              return;
    }
    char ch = 0;
    std::ostringstream str;
    while(file.peek() != EOF){
                       file.get(ch);
                       str << ch;
    }

    std::string temp = str.str();
    std::string temp_link;

    unsigned int curr = 0;
    for(unsigned int i = 0;i<temp.length();i++){
            if(temp.at(i) == '\n'){
                          add_link(std::string(temp,curr,i-curr),type);
                          curr = i + 1;
                          }

    }
    add_link(std::string(temp,curr,temp.length()-curr),type);
}

std::string net_list::to_ip4(char ip[])
{
     char ipp[8];
     memset(ipp, 0, sizeof(ipp));
     sprintf(ipp, "%c%c%c%c%c%c%c%c",
                        ip[6],  ip[7],  ip[4],  ip[5],
                        ip[2],  ip[3],  ip[0],  ip[1]);
     std::ostringstream str;
     int one=0,two=0,three=0,four=0;
     sscanf(ipp,"%2X%2X%2X%2X",&one,&two,&three,&four);
     str << one;
     str << ".";
     str << two;
     str << ".";
     str << three;
     str << ".";
     str << four;
     return str.str();
}

void net_list::add_link(std::string link,std::string type)
{
     if( link.find("sl",0) != std::string::npos){
    	 return ;
     }
     unsigned int nID=0, nLPort=0, nRPort=0, nStatus=0;
     char local_address[9];
     char remote_address[9];
     memset(local_address, 0, sizeof(local_address));
     memset(remote_address, 0, sizeof(remote_address));

     sscanf(link.c_str(), " %d: %8[0-9A-Fa-f]:%X %8[0-9A-Fa-f]:%X %X ",
                &nID,
                local_address,
                &nLPort,
                remote_address,
                &nRPort,
                &nStatus
                );

     if( (nLPort == 0) || (nRPort == 0)){
     	return ;
     }else{
     	struct net_link temp={type,to_ip4(local_address),nLPort,to_ip4(remote_address),nRPort,STATUS[nStatus]};
     	push_back(temp);
     }
}

